import {
  Column,
  Entity,
  Index,
  JoinTable,
  ManyToMany,
  OneToMany,
} from 'typeorm';
import { Exclude } from 'class-transformer';
import { Role } from '../../role/entities/role.entity';
import { CommonEntity } from '../../../../util/entity/common.entity';
import { IsEmail } from 'class-validator';
import { Bookmark } from '../../../my/bookmark/entities/bookmark.entity';
import { Money } from '../../../game/money/entities/money.entity';
import { UserItem } from '../../../game/user-item/entities/user-item.entity';

@Entity('sys_user')
export class User extends CommonEntity {
  @Column({ comment: '用户名,唯一' })
  @Index({ unique: true })
  username: string;

  @Exclude()
  @Column({ comment: '密码' })
  password: string;

  @Column({ nullable: true, length: 30, comment: '昵称' })
  nickname: string;

  @Column({ type: 'tinyint', default: 1, comment: '性别1-男 2-女' })
  gender: number;

  @Column({ nullable: true, comment: '头像' })
  avatar: string;

  @IsEmail()
  @Column({ nullable: true, comment: '邮箱' })
  email: string | null;

  @OneToMany(() => Bookmark, (bookmark) => bookmark.user)
  bookmarks: Bookmark[];

  @ManyToMany(() => Role, (role) => role.users)
  @JoinTable({ name: 'sys_user_role' })
  roles: Role[];

  @OneToMany(() => Money, (money) => money.user)
  moneys: Money[];

  @OneToMany(() => UserItem, (userItem) => userItem.user)
  userItems: UserItem[];
}
